home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / itcl1_31.z / itcl1_31 / tcldev / itcl-1.3 / tests / inherit.test < prev    next >
Encoding:
Text File  |  1993-10-15  |  5.1 KB  |  194 lines

  1. #
  2. # Tests for inheritance and scope handling
  3. # ----------------------------------------------------------------------
  4. #   AUTHOR:  Michael J. McLennan       Phone: (215)770-2842
  5. #            AT&T Bell Laboratories   E-mail: aluxpo!mmc@att.com
  6. #
  7. #     SCCS:  @(#)inherit.test    1.5 (10/14/93)
  8. # ----------------------------------------------------------------------
  9. #            Copyright (c) 1993  AT&T  All Rights Reserved
  10. # ======================================================================
  11.  
  12. # ----------------------------------------------------------------------
  13. #  MULTIPLE BASE-CLASS ERROR DETECTION
  14. # ----------------------------------------------------------------------
  15. test {Cannot inherit from the same base class more than once} {
  16.     catch "VirtualErr" errmsg
  17.     set errmsg
  18. } {
  19.     $result == {error while autoloading "VirtualErr": class "VirtualErr" inherits base class "Foo" more than once:
  20.   VirtualErr->Mongrel->FooBar->Foo
  21.   VirtualErr->Foo
  22.   VirtualErr->BarFoo->Foo}
  23. }
  24.  
  25. # ----------------------------------------------------------------------
  26. #  CONSTRUCTION
  27. # ----------------------------------------------------------------------
  28. test {Constructors should be invoked implicitly} {
  29.     set WATCH ""
  30.     concat [Mongrel m] / $WATCH
  31. } {
  32.     $result == "m / Mongrel FooBar Foo Bar Geek"
  33. }
  34.  
  35. test {Initialization of shadowed variables works properly} {
  36.     concat [m info public blit -value] / [m info public Foo::blit -value]
  37. } {
  38.     $result == "nonnull / <undefined>"
  39. }
  40.  
  41. # ----------------------------------------------------------------------
  42. #  PUBLIC VARIABLES
  43. # ----------------------------------------------------------------------
  44. test {Inherited "config" method works on derived classes} {
  45.     m config -blit xyz -Foo::blit pdq
  46. } {
  47.     $result == "Mongrel::blit Foo::blit"
  48. }
  49.  
  50. test {Inherited "config" method works on derived classes} {
  51.     m config -blit xyz -Foo::blit pdq
  52.     concat [m info public blit -value] / [m info public Foo::blit -value]
  53. } {
  54.     $result == "xyz / pdq"
  55. }
  56.  
  57. # ----------------------------------------------------------------------
  58. #  INHERITANCE INFO
  59. # ----------------------------------------------------------------------
  60. test {Info: class} {
  61.     m info class
  62. } {
  63.     $result == "Mongrel"
  64. }
  65.  
  66. test {Info: inherit} {
  67.     m info inherit
  68. } {
  69.     $result == "FooBar Geek"
  70. }
  71.  
  72. test {Info: heritage} {
  73.     m info heritage
  74. } {
  75.     $result == "Mongrel FooBar Foo Bar Geek"
  76. }
  77.  
  78. test {Built-in "isa" method} {
  79.     set status 1
  80.     foreach c [m info heritage] {
  81.         set status [expr {$status && [m isa $c]}]
  82.     }
  83.     set status
  84. } {
  85.     $result == 1
  86. }
  87.  
  88. test {Built-in "isa" method} {
  89.     m isa Watermelon
  90. } {
  91.     $result == 0
  92. }
  93.  
  94. # ----------------------------------------------------------------------
  95. #  SCOPE MANIPULATION
  96. # ----------------------------------------------------------------------
  97. test {commands normally execute in the scope of their class} {
  98.     m Foo::do {info class}
  99. } {
  100.     $result == "Foo says 'Foo'"
  101. }
  102.  
  103. test {"virtual" command moves scope to most specific class} {
  104.     m Foo::do {virtual info class}
  105. } {
  106.     $result == "Foo says 'Mongrel'"
  107. }
  108.  
  109. test {"previous" command moves scope upward in hierarchy} {
  110.     m do {virtual previous info class}
  111. } {
  112.     $result == "Foo says 'FooBar'"
  113. }
  114.  
  115. test {"previous" command can be chained} {
  116.     m do {virtual previous previous info class}
  117. } {
  118.     $result == "Foo says 'Foo'"
  119. }
  120.  
  121. # ----------------------------------------------------------------------
  122. #  METHOD INVOCATION
  123. # ----------------------------------------------------------------------
  124. test {Simple method names are assigned based on heritage} {
  125.     m do {concat "$this ([virtual info class]) at scope [info class]::"}
  126. } {
  127.     $result == "Foo says 'm (Mongrel) at scope Foo::'"
  128. }
  129.  
  130. test {Explicit scoping can be used to reach shadowed members} {
  131.     m Geek::do {concat "$this ([virtual info class]) at scope [info class]::"}
  132. } {
  133.     $result == "Geek says 'm (Mongrel) at scope Geek::'"
  134. }
  135.  
  136. test {Methods execute in local scope of class, e.g., Foo::do} {
  137.     m config -blit abc -Foo::blit def
  138.     m Foo::do {set blit xyz}
  139.     concat [m info public blit -value] / [m info public Foo::blit -value]
  140. } {
  141.     $result == "abc / xyz"
  142. }
  143.  
  144. # ----------------------------------------------------------------------
  145. #  DESTRUCTION
  146. # ----------------------------------------------------------------------
  147. test {Destructors should be invoked implicitly} {
  148.     set WATCH ""
  149.     concat [m delete] / $WATCH
  150. } {
  151.     $result == "/ Mongrel Geek Bar Foo FooBar"
  152. }
  153.  
  154. # ----------------------------------------------------------------------
  155. #  OBJECT INFO
  156. # ----------------------------------------------------------------------
  157. foreach obj [itcl_info objects] {
  158.     $obj delete
  159. }
  160. Mongrel m
  161. FooBar fb
  162. Foo f
  163. Geek g
  164.  
  165. test {Object queries can be restricted by object name} {
  166.     itcl_info objects f*
  167. } {
  168.     [test_cmp_lists $result {f fb}]
  169. }
  170.  
  171. test {Object queries can be restricted to specific classes} {
  172.     itcl_info objects -class Foo
  173. } {
  174.     $result == "f"
  175. }
  176.  
  177. test {Object queries can be restricted by object heritage} {
  178.     itcl_info objects -isa Foo
  179. } {
  180.     [test_cmp_lists $result {m f fb}]
  181. }
  182.  
  183. test {Object queries can be restricted by object name / specific classes} {
  184.     itcl_info objects f* -class Foo
  185. } {
  186.     $result == "f"
  187. }
  188.  
  189. test {Object queries can be restricted by object name / object heritage} {
  190.     itcl_info objects f* -isa Foo
  191. } {
  192.     [test_cmp_lists $result {f fb}]
  193. }
  194.